package com.sphchina.esb.webservice;

import com.javaweb.common.utils.JsonResult;
import com.sphsine.bip.BaseRequest;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

/**
 * NCRequest
 *
 * @author HanQuinn
 * Date: 2024/3/19
 * Time: 13:48
 */
@Service
public class NCRequest extends BaseRequest implements INCRequset{

    private HttpClient client;

    @Override
    public JsonResult callToNC(String body, String suffixUrl) throws Exception {
            String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:isw=\"http://resources.pu.itf.nc/ISWYToNCInvoice\">\\r<soapenv:Header/>\\r<soapenv:Body>\\r<isw:addInvoice>\\r<string>" +
                    body
                    + "</string>\\r</isw:addInvoice>\\r</soapenv:Body>\\r</soapenv:Envelope>";
            HttpClient httpClient = client == null ? HttpClientBuilder.create()
                    .setConnectionManager(super.getHttpClientConnectionManager(false))
                    .build() : this.client;
            HttpRequestBase httpPost = new HttpPost("http://10.11.0.109:80" + suffixUrl);
            RequestConfig requestConfig = RequestConfig.custom().build();
            httpPost.setConfig(requestConfig);
            StringEntity postEntity = new StringEntity(requestBody, "UTF-8");
            httpPost.addHeader("Content-Type", "application/xml");
            ((HttpPost) httpPost).setEntity(postEntity);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            String str = EntityUtils.toString(httpEntity, "UTF-8");
            return str.contains("\"code\":\"000\"")? JsonResult.success():JsonResult.error();
    }
}
